/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.massage;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.UserMapper;
import main_com.pojo.LoginBean;
import main_com.pojo.LookMessageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/UpdateMessageController")
public class UpdateMessageController {
//    private final UserMapper userMapper;
//
//    public UpdateMessageController(UserMapper userMapper) {
//        this.userMapper = userMapper;
//    }

    //    public void wrong1(){
//        String msg="不允许有空，修改失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void right(){
//        String msg="填写信息合格，修改成功！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/updateMessage")
    protected void updateMessage(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String edu = request.getParameter("edu");
        String work = request.getParameter("work");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        if (phone.isEmpty() || email.isEmpty()) {
//            wrong1();
            response.sendRedirect("http://localhost/lookMessage/updateMessage.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.isEmpty()) {
                    response.sendRedirect("http://localhost/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                userMapper.updateUserEWPEByUserName(edu, work, phone, email, userName);
                LookMessageBean mm = userMapper.getMessageBeansByUserName(userName).get(0);

                ArrayList<LookMessageBean> wordlist = new ArrayList<>();
                wordlist.add(mm);
                session.setAttribute("wordlist", wordlist);
//                right();
                response.sendRedirect("http://localhost/lookMessage/lookMessage.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
